﻿// LightNet - Lightweight .NET Framework
// Copyright (c) 2010 http://lightnet.codeplex.com

namespace System.Runtime.CompilerServices
{
    using System.Reflection;

    /// <summary>
    /// Specifies the details of how a method is implemented. This class cannot be inherited.
    /// </summary>
    [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
    public sealed class MethodImplAttribute : Attribute
    {
        public MethodCodeType MethodCodeType;

        public MethodImplOptions Value { get; internal set; }

        /// <summary>
        /// Initializes a new instance of the MethodImplAttribute class.
        /// </summary>
        public MethodImplAttribute()
        {
        }

        /// <summary>
        /// Initializes a new instance of the MethodImplAttribute class with the specified <see cref="T:System.Runtime.CompilerServices.MethodImplOptions" /> value.
        /// </summary>
        /// <param name="value">A bitmask representing the desired <see cref="T:System.Runtime.CompilerServices.MethodImplOptions" /> value which specifies properties of the attributed method.</param>
        public MethodImplAttribute(short value)
        {
            Value = (MethodImplOptions)value;
        }

        /// <summary>
        /// Initializes a new instance of the MethodImplAttribute class with the specified <see cref="T:System.Runtime.CompilerServices.MethodImplOptions" /> value.
        /// </summary>
        /// <param name="methodImplOptions">A <see cref="T:System.Runtime.CompilerServices.MethodImplOptions" /> value specifying properties of the attributed method.</param>
        public MethodImplAttribute(MethodImplOptions methodImplOptions)
        {
            Value = methodImplOptions;
        }

        internal MethodImplAttribute(MethodImplAttributes methodImplAttributes)
        {
            var options = MethodImplOptions.InternalCall | MethodImplOptions.PreserveSig | MethodImplOptions.NoOptimization | MethodImplOptions.Synchronized | MethodImplOptions.ForwardRef | MethodImplOptions.NoInlining | MethodImplOptions.Unmanaged;
            Value = ((MethodImplOptions)methodImplAttributes) & options;
        }
    }
}
